package com.nidu.demo.oauth2.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.oauth2.convertor.OAuth2AccessTokenCriteriaConvertor;
import com.nidu.demo.oauth2.dto.OAuth2AccessTokenPageQry;
import com.nidu.demo.oauth2.convertor.OAuth2AccessTokenConvertor;
import com.nidu.demo.oauth2.dto.OAuth2AccessTokenCO;
import com.nidu.demo.oauth2.gateway.OAuth2AccessTokenGateway;
import com.nidu.demo.oauth2.model.OAuth2AccessToken;
import com.nidu.demo.oauth2.model.OAuth2AccessTokenCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class OAuth2AccessTokenPageQryExe {

    private final OAuth2AccessTokenGateway gateway;

    private final OAuth2AccessTokenConvertor convertor;

    private final OAuth2AccessTokenCriteriaConvertor criteriaConvertor;

    public PageResponse<OAuth2AccessTokenCO> execute(OAuth2AccessTokenPageQry qry) {
        OAuth2AccessTokenCriteria criteria = criteriaConvertor.toDataObject(qry);
        PageResponse<OAuth2AccessToken> page = gateway.pageByCondition(criteria);
        List<OAuth2AccessTokenCO> tokenCOS = convertor.toClientObjects(page.getData());
        return PageResponse.of(tokenCOS, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }
}
